package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.EditAddrActivity;
import com.xgkj.diyiketang.activity.MyAddressActivity;
import com.xgkj.diyiketang.data.AddressInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.DeleteAddressTask;
import com.xgkj.diyiketang.util.DialogUtil;
import com.xgkj.diyiketang.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by windo on 2015/12/18.
 */
public class MyAddrAdapter extends BaseAdapter {

    private Context mContext;
    private String addressId;
    private int deleteTaskId = -1;
    private List<AddressInfo> mList = new ArrayList<AddressInfo>();

    public MyAddrAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<AddressInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList != null) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.myaddr_item, parent, false);
            childHolder.nickNameText = (TextView) convertView.findViewById(R.id.nickname);
            childHolder.phoneText = (TextView) convertView.findViewById(R.id.phone);
            childHolder.addrText = (TextView) convertView.findViewById(R.id.sh_addr);
            childHolder.editLayout = (RelativeLayout) convertView.findViewById(R.id.edit_layout);
            childHolder.deletelayout = (RelativeLayout) convertView.findViewById(R.id.delete_layout);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        AddressInfo info = mList.get(position);
        if (info != null) {
            childHolder.nickNameText.setText(info.getAddressUser());
            childHolder.phoneText.setText(info.getMobile());
            childHolder.addrText.setText(info.getAddress());
        }


        childHolder.editLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, EditAddrActivity.class);
                AddressInfo einfo = mList.get(position);
                intent.putExtra("ID",einfo.getId());
                intent.putExtra("NAME",einfo.getAddressUser());
                intent.putExtra("PHONE",einfo.getMobile());
                intent.putExtra("ADDR",einfo.getAddress());
                mContext.startActivity(intent);
            }
        });

        childHolder.deletelayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.createDialog(mContext, -1, mContext.getString(R.string.prompt), mContext.getString(R.string.sure_delete), mContext.getString(R.string.ok), mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        ToastUtil.showMessage(mContext, "测试删除");
                        doDelete(position);
                    }
                }, null);
            }
        });

        return convertView;
    }

    private void doDelete(int position){
        AddressInfo ainfo = mList.get(position);
        addressId = ainfo.getId();
        DeleteAddressTask task = new DeleteAddressTask(addressId);
        task.addListener(toupiaoListener);
        deleteTaskId = EatShowApp.getInstance().getTaskManager().addTask(task);
    }

    private TaskEventListener toupiaoListener = new ShowToastTaskEventListener(mContext){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            ((MyAddressActivity)mContext).pageNo = 1;
            ((MyAddressActivity)mContext).getMyOrderList();
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };
    class ChildHolder {
        TextView nickNameText;
        TextView phoneText;
        TextView addrText;
        RelativeLayout editLayout;
        RelativeLayout deletelayout;
    }
}
